**** Synthetic control placebo testing 

*** Dates for the defendants
local StJoTreat = mofd(mdy(11,16,2004))
local StJoSettle = mofd(mdy(12,21,2007))

local WheatonTreat = mofd(mdy(10,19,2004))
local WheatonSettle = mofd(mdy(1,7,2010))

local ElCentroTreat = mofd(mdy(3,20,2006))
local ElCentroSettle = mofd(mdy(9,20,2010))

local OverlookTreat = mofd(mdy(4,25,2008))
local OverlookSettle = mofd(mdy(6,21,2012))

local MortonPlantTreat = mofd(mdy(8,27,2008)) 
local MortonPlantSettle = mofd(mdy(11,20,2012))

local ShandsTreat = mofd(mdy(8,30,2008))
local ShandsSettle = mofd(mdy(8,8,2013))

local chsTreat = mofd(mdy(1,7,2009))
local chsSettle= mofd(mdy(8,4,2014))



*** Loop over defendants
local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
foreach dft of local dftlist {


	matrix deterrence = (0,0)
	cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop
	
	** Set up the data
	use ipop_controls_panel.dta, clear
	keep if prvdrnum == "`dft'"
	drop control
	append using "controls/`dft'_controls.dta"

	*** Encode controls and treated variable
	encode(prvdrnum), gen(group)
	tsset group dt
	count if group == 999
	assert(r(N) == 0)
	replace group = 999 if prvdrnum == "`dft'"

	*** Don't save prvdrnum group crosswalk because we won't report the synth weights

	** Save this panel in placebo folder
	save "placebos/`dft'_panel.dta", replace 

	drop if group == 999 
	levelsof group, local(placebos)

	*** Loop over the placebo for that defendant
	foreach p of local placebos{

		cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/placebos
		use `dft'_panel.dta, clear  
		drop if group == 999 

		***********************************
		* Swap in placebos
		disp "Placebo group `p'"
		replace group = 999 if group == `p'
		save `dft'_panel_temp.dta, replace 



		*** Prep for looping leads-lags
		levelsof group, local(levels)
		keep group dt pmt_amt 
		reshape wide pmt_amt, i(dt) j(group)
		tsset dt
		mata: distanceLead= J(1,3,0)
		mata: distanceLag = J(1,3,0)

		* 3 columns: group, lag period, distance

		set matsize 11000


		** Only want to keep treated pre-period 
		local treatment_period = ``dft'Treat'
		replace pmt_amt999 = . if dt >= `treatment_period'


		** Find max lead so that there are 5 years of post-period data 
		summarize dt 
		local maxlead = r(max) - `treatment_period' + 1 - 60  
		** Max - treatment  + 1 = length of end window 
		** Take 60 off and that's the max you can shift before running out of data

		** Find max lag so that there are 3 years of pre-period data 
		summarize dt 
		local maxlag = `treatment_period' - r(min) - 36
		** Treatment period - min  = length of end window 

		*** Generate series of 0s for use in computing diff
		gen diff = 0 

		*** Loop, storing leads lags and fit
		foreach g of local levels {
			disp "Looping for defendant `dft', control `g'"
			* Skip treated group
			if `g' == 999{
					continue
				}

			foreach t of numlist 0/`maxlag'{
				*disp `t'
				qui replace diff = (pmt_amt999 - L`t'.pmt_amt`g')^2
				qui summarize diff 
				local dist = r(sum)/r(N)
				mata: distanceLag=  distanceLag \ `g', `t', `dist'
				}

			foreach t of numlist 0/`maxlead'{
				* disp `t'
				qui replace diff = (pmt_amt999 - F`t'.pmt_amt`g')^2
				qui summarize diff 
				local dist = r(sum)/r(N)
				mata: distanceLead=  distanceLead \ `g', `t', `dist'
				}
		}



		mata: st_matrix("distanceLead", distanceLead)
		mata: st_matrix("distanceLag", distanceLag)


		svmat distanceLag
		keep distanceLag*
		svmat distanceLead

		rename distanceLag1 laggroup
		rename distanceLag2 lagperiod
		rename distanceLag3 lagdistance
		rename distanceLead1 leadgroup
		rename distanceLead2 leadperiod
		rename distanceLead3 leaddistance

		drop if laggroup == 0

		*** Don't save, imediately use 

		********************************************************************************
		*** Find best fit per group


		preserve
		keep laggroup lagperiod lagdistance
		drop if mi(laggroup)
		bysort laggroup: egen minlagdistance = min(lagdistance)
		keep if lagdistance == minlagdistance
		* Get rid of dupes
		collapse (min) lagperiod (min) lagdistance, by(laggroup)
		rename laggroup group
		save "group_lag_mindistance_`dft'_temp.dta", replace

		restore
		keep leadgroup leadperiod leaddistance
		drop if mi(leadgroup)
		bysort leadgroup: egen minleaddistance = min(leaddistance)

		keep if leaddistance == minleaddistance
		** Get rid of dupes 
		sort leadgroup leadperiod
		collapse (min) leadperiod (min) leaddistance, by(leadgroup)
		rename leadgroup group
		save "group_lead_mindistance_`dft'_temp.dta", replace

		clear
		use "group_lag_mindistance_`dft'_temp.dta"
		merge 1:1 group using "group_lead_mindistance_`dft'_temp.dta"

		drop _merge
		bysort group: gen mindistance = min(leaddistance, lagdistance)

		replace lagdistance = . if lagdistance!= mindistance
		replace leaddistance = . if leaddistance!= mindistance

		replace lagperiod = . if lagdistance == .
		replace leadperiod = . if leaddistance == .

		replace lagperiod = -1*lagperiod
		gen period = min(lagperiod, leadperiod)
			* Min because missing is infty 

		keep group period mindistance
		drop if mi(group)
		save "groupleadlags_`dft'_temp.dta", replace

		*** Run Synthetic controls on fit lead/lags

		use `dft'_panel_temp.dta, clear 


		** Set as time series 
		tsset group dt 

		*** Merge leads and lags
		merge m:1 group using "groupleadlags_`dft'_temp.dta"
		drop _merge mindistance


		** Store group levels in macro
		levelsof group, local(levels)


		*** Replace with fitted lead/lags
		sort group dt
		gen out = .


		foreach g of local levels{
			disp `g'
			* Placebo group gets no time shift
			if `g' == 999{
				replace out = pmt_amt if group == `g'
				continue
				}

			** Grab the stored shifter 
			qui summarize period if group == `g'
			local shift = r(min)

			** If positive, create lead 
			if `shift' > 0{
				replace out = F`shift'.pmt_amt if group == `g'
			}
			** If negative, create lag 
			if `shift' < 0{
				local shiftvalue = abs(`shift')
				replace out = L`shiftvalue'.pmt_amt if group == `g'
			}

			if `shift' == 0{
				replace out = pmt_amt if group == `g'
			}
		}




		*** Treatment date is filing date for the defendant 
		local treatment_period = ``dft'Treat'

		** Cut missing data from front of time series
		** Which is limited by the highest lag 
		summarize period
		drop if dt < mofd(mdy(1,1,1999)) + abs(r(min))

		*** Cut missing data from end of time series
		*** Which is limited by furthest lead
		*** Data end end of 2016
		summarize period
		drop if dt > mofd(mdy(12, 31, 2016)) - abs(r(max))

		synth out out, trunit(999) trperiod(`treatment_period') figure

		*** Store fitted values
		matrix fit = e(Y_synthetic)
		keep if group == 999
		svmat fit 
		save "synth_fit_`dft'_temp.dta", replace 
		
		*** Compute deterrence
		cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/placebos
		use `dft'_panel_temp.dta
		keep if group == 999 
		merge 1:1 dt using "synth_fit_`dft'_temp.dta"

		local treatment_period = ``dft'Treat'


		** Produce difference in series
		gen deterrence = fit1 - out if dt >= `treatment_period'

		** Time discount the difference at 10% 
		local r = 1.1^(1/12)

		gen deterrence_disc = deterrence*1/(`r'^(dt-`treatment_period'))
		replace deterrence_disc = . if dt-`treatment_period' >= 60
		count if !mi(deterrence_disc)
		assert(r(N)==60)

		*** Compute total
		collapse (sum) deterrence_disc
		qui summarize deterrence_disc
		local deterrence = r(min)

		matrix deterrence = (deterrence\ `p', `deterrence')

		rm "synth_fit_`dft'_temp.dta"
		rm "groupleadlags_`dft'_temp.dta"
		rm "group_lead_mindistance_`dft'_temp.dta"
		rm "group_lag_mindistance_`dft'_temp.dta"
		rm `dft'_panel_temp.dta
	}

	clear 
	svmat deterrence
	rename deterrence1 group 
	rename deterrence2 deterrence
	sort deterrence
	save deterrence_dist_`dft'.dta, replace
}

*** Count placebos
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/placebos
log using placebo_p.log, replace
* StJo:
use deterrence_dist_StJo.dta, clear 
drop if group == 0 
count 
count if deterrence >= 44813236

* Wheaton
use deterrence_dist_Wheaton.dta, clear 
drop if group == 0 
count
count if deterrence >= 5848177.4

* ElCentro Deterrence:
use deterrence_dist_ElCentro.dta, clear 
drop if group == 0 
count
count if deterrence >= 5273055.7

* Overlook
use deterrence_dist_Overlook.dta, clear 
drop if group == 0 
count
count if deterrence <= -15963478

** MortonPlant
use deterrence_dist_MortonPlant.dta, clear 
drop if group == 0 
count 
count if deterrence >=  2.666*10^8

* Shands 
use deterrence_dist_Shands.dta, clear  
drop if group == 0 
count 
count if deterrence >= 1.242*10^8

** CHS
use deterrence_dist_chs.dta, clear  
drop if group == 0 
count 
count if deterrence >= 6.932e+08

log close 






